package cn.wolfcode.handler;

import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.redis.SeckillRedisKey;
import com.alibaba.fastjson.JSON;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.ORB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Slf4j
@CanalTable(value = "t_order_info")
@Component
public class OrderInfoHandler implements EntryHandler<OrderInfo> {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public void insert(OrderInfo orderInfo) {
        System.out.println("插入订单===>" +orderInfo);
        // 订单插入的时候
        // 设置OrderSet
        // 3.将秒杀商品的id和用户进行关联===>Set
        String orderSetKey = SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(orderInfo.getSeckillId() + "");
        redisTemplate.opsForSet().add(orderSetKey,orderInfo.getPhone());
        // 将OrderInfo通过Hash存储到redis
        String orderHshKey = SeckillRedisKey.SECKILL_PRODUCT_HASH.getRealKey("");
        redisTemplate.opsForHash().put(orderHshKey,orderInfo.getOrderNo(), JSON.toJSONString(orderInfo));
    }

    @Override
    public void update(OrderInfo before, OrderInfo after) {
        System.out.println("更新前===>" +before);
        System.out.println("=======================");
        System.out.println("更新后===>" +after);
        // 更新redis中订单的缓存信息
        String orderHshKey = SeckillRedisKey.SECKILL_PRODUCT_HASH.getRealKey("");
        redisTemplate.opsForHash().put(orderHshKey,after.getOrderNo(),JSON.toJSONString(after));
    }

    @Override
    public void delete(OrderInfo orderInfo) {

    }
}
